<?php
/**
 * 文章管理 逻辑层
 */

namespace App\Http\Repositories\Admin\Article;
use App\Exceptions\ApiException;
use App\Exceptions\ValidatorException;
use App\Http\Repositories\BaseRepository as Repository;
use App\Models\Admin\ArticleModel;

class ArticleRepository extends  Repository
{
    private $article;
    public function __construct(ArticleModel $article)
    {
        $this->article = $article;
    }

    /**
     * 查询处理
     * @param $data
     * @return array
     */
    public function handleCondition($data)
    {
        is_real_exists(@$data['title']) && $where['title'] = ['like', "%".$data['title']."%"];
        is_real_exists(@$data['article_type']) && $where['article_type'] = $data['article_type'];
        is_real_exists(@$data['create_time']) && $where['create_time'] = ['between', $data['create_time']];

        return $where ?? [];
    }

    /**
     * 列表
     * @param $data
     * @return mixed
     */
    public function getArticleList($data)
    {
        $where = $this->handleCondition($data);

        return $this->article->getListByPage($where,  ['*'], config('pageSize'));
    }

    /**
     * 详情
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function getArticleInfo($data)
    {
        $where['id'] = $data['id'];
        return $this->article->getInfo($where);
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function addArticle($data)
    {
        $data['author'] = config('userInfo')['member_name'];
        $data['author_uuid'] = config('userInfo')['uuid'];

        return $this->article->addItem($data);
    }

    /**
     * 删除
     * @param $data
     * @return array
     */
    public function delArticle($data)
    {
        return $this->article->deleteItem($data);
    }

    /**
     * 编辑
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function editArticle($data)
    {
        $where['id'] = $data['id'];
        $article = $this->article->getInfo($where);

        $article->update($data);

        return $article;
    }

}
